package com.guigu.basedservice.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.basedservice.dto.BasePartsTypeDTO2;
import com.guigu.basedservice.dto.BasePartsTypeDto;
import com.guigu.basedservice.entity.BasePartsType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 配件目录 Mapper 接口
 * </p>
 *
 * @author Heyuhang
 * @since s2024-09-13
 */
@Mapper
public interface BasePartsTypeMapper extends BaseMapper<BasePartsType> {

    /**
     * 查找所有父节点为指定ID的节点
     * @param parentId
     * @return
     */
    List<BasePartsType> findByParentId(Long parentId);

    List<BasePartsType> queryAll();

    int addManyBasePartsType(List<BasePartsType> list);

//    List<BasePartsType> findByCondition2(QueryWrapper<BasePartsType> wrapper);



//    List<BasePartsType> findByParentTypeUuid(String parentTypeUuid);

    // 获取根节点（即PARENT_TYPE_UUID为null的节点）
    @Select("SELECT PART_TYPE_UUID AS id, NAME, (CASE WHEN EXISTS (SELECT 1 FROM base_parts_type WHERE PARENT_TYPE_UUID = t.PART_TYPE_UUID) THEN 0 ELSE 1 END) AS isLeaf FROM base_parts_type t WHERE PARENT_TYPE_UUID IS NULL")
    List<BasePartsType> getRootNodes();

    // 获取子节点（根据PARENT_TYPE_UUID查找）
    @Select("SELECT \n" +
            "    PART_TYPE_UUID AS id, \n" +
            "    NAME, \n" +
            "    (CASE \n" +
            "        WHEN EXISTS (\n" +
            "            SELECT 1 \n" +
            "            FROM base_parts_type \n" +
            "            WHERE PARENT_TYPE_UUID = t.PART_TYPE_UUID\n" +
            "        ) THEN 0 \n" +
            "        ELSE 1 \n" +
            "    END) AS isLeaf \n" +
            "FROM base_parts_type t \n" +
            "WHERE PARENT_TYPE_UUID = #{parentId} ")
    List<BasePartsType> getChildNodes(@Param("parentId") String parentId);


    List<BasePartsType> getRecursiveSql();


}
